<?php require_once(dirname(__FILE__) . '/../include/config.inc.php');
require_once "function.php";

/*
进度：
0 已经撤单
1 报修成功
2 客服已经联系
3 系统已经派单
4 工程师接单
5 维修完成
6 已经评价
*/
$uid = $_SESSION['uid'];
$userInfo = "";
if (isset($uid)) {

    //有session 从数据库中读取用户名信息
    //$openid_str=$_SESSION['openid'];
    $sql = "SELECT u.*,g.groupname  as groupname FROM `#@__user` as u 
				   left join `#@__usergroup` as g  on u.level=g.id  
				   WHERE u.id='$uid'  ";
    $row = $dosql->GetOne($sql);
    if (is_array($row) && isset($row['id'])) {
        // 返回用户 id
        $id = $row['id'];
        $_SESSION['id'] = $id;
        $userInfo = $row;
        $level = intval($row['level']);
    }
}

if ($level > 5) {
    exit();
}


$projectid = empty($projectid) ? "" : intval($projectid);
if (isset($projectid)) {


    $row = $dosql->GetOne("SELECT * FROM `#@__project`  WHERE id='$projectid'  AND is_delete=0 ");
    if (is_array($row) && isset($row['id'])) {
        $jindu2 = intval($row['jindu']);
        $signIn = intval($row['signIn']);
        $pmid = $row['pmid'];
        $old_manageid = $row['manageid'];
        // 返回工单数据
        /*
        进度：
        0 已经撤单
        1 报修成功
        2 客服已经联系
        3 系统已经派单
        4 工程师接单
        5 维修完成
        6 已经评价
        */
        $op_str = "";
        //判断是否是该工单的执行人
        if ($uid == $pmid) {
            if ($jindu2 == 3) {
                $op_str = '
			<div class="weui-flex__item">
			<div class="placeholder"><a class="weui-btn weui-btn_primary" id="jiedan_btn"  href="javascript:" onClick="jiedan(' . $row['id'] . ')"><i class="fa fa-thumb-tack" aria-hidden="true"></i>    接单</a></div>
			</div>
			
			';
            } elseif ($jindu2 == 4) {

                if ($signIn == 1) {
                    $op_str = '<div class="weui-flex__item"><div class="placeholder"><a class="weui-btn weui-btn_warn" href="javascript:;" onclick="qiantui2(' . $row['id'] . ')" ><i class="fa fa-pencil" aria-hidden="true"></i>  中途人员签退</a></div></div>';
                }


            }


        }


        //故障图片s
        $pic_lists = "";
        $previewImage_url = array();
        if ($row['picfiles'] != "") {
            $picurl_arr = json_decode($row['picfiles'], TRUE);
            for ($i = 0; $i < count($picurl_arr); $i++) {
                $previewImage_url[] = $picurl_arr[$i];
                $pic_lists .= '<li class="weui-uploader__file" style="background-image:url(' . $picurl_arr[$i] . ')"><img src="' . $picurl_arr[$i] . '" width="0"/></li>';
            }
        }

        //完修 图片
        $over_picurls = $row['over_picurls'];
        $over_remark = $row['over_remark'];
        $fee = $row['fee'];


    } else {
        echo "<script>alert('任务不存在或者已经删除！')</script>";
        exit();
    }
}
?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>上传施工周汇报--<?php echo $cfg_webname; ?></title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="description" content="<?php echo $cfg_webname; ?>">
        <link rel="stylesheet" href="lib/weui.min.css">
        <link rel="stylesheet" href="css/jquery-weui.css">
        <link rel="stylesheet" href="css/font-awesome.min.css">
        <link rel="stylesheet" href="css/demos.css?v=20201212.1626">
        <link rel="stylesheet" href="/admin/ui/css/bootstrap.min.css">
        <link rel="stylesheet" href="/admin/ui/css/bootstrap-theme.min.css">
    </head>

<body ontouchstart>
<div class="top">
    <div class="user_top user_project">
        <i>
            <?php
            if (intval($level) > 1) {
                echo '<a href="manage.php">' . $userInfo['groupname'] . '</a>';
            } else {
                echo $userInfo['groupname'];
            } ?>
            UID:<?php echo $id; ?>
        </i>
        <div class="user_top_conn"><img src="<?php echo $userInfo['headimgurl'] ?>"/>
            <p><?php echo $userInfo['truename'] ?></p>
            <?php if (intval($level) < 5) { ?><a class="user_link" href="member.php">返回用户</a><?php } ?>
        </div>
    </div>
</div>
<!--工单列表开始-->

<!--工单详情开始-->
<div class="main">
    <div class="weui-cells__title"><i class="fa fa-id-card-o" aria-hidden="true"></i>工程信息

    </div>
    <div class="weui-form-preview">
        <div class="weui-form-preview__bd">
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">工程编号：</label>
                <span class="weui-form-preview__value">
		<?php
        if (!empty($row['poster'])) {
            echo '<i class="fa fa-smile-o blue "></i>';
        }
        if (!empty($row['openid'])) {
            echo '<i class="fa fa-weixin green"></i>';
        }
        ?>
        <?php echo date("Ymd", $row['post_time']) . "-" . $row['id'] ?>
        </span>
            </div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">工程名称：</label>
                <span class="weui-form-preview__value"><?php echo $row['projectname'] ?></span></div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">甲方：</label>
                <span class="weui-form-preview__value"><?php echo $row['school'] ?></span></div>
            <?php if (!empty($row['truename'])) { ?>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">联系人：</label>
                    <span class="weui-form-preview__value"><?php echo $row['truename'] ?></span></div>
            <?php } ?>
            <?php if (!empty($row['mobile'])) { ?>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">电话：</label>
                    <span class="weui-form-preview__value"><a href="tel:<?php echo $row['mobile'] ?>"><i class="fa fa-mobile"><?php echo $row['mobile'] ?></a></i></span>
                </div>
            <?php } ?>
            <?php if (!empty($row['mobile'])) { ?>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">工程位置：</label>
                    <span class="weui-form-preview__value" id="daohang" data-projectid="<?php echo $row['id'] ?>"><i class="fa fa-map-marker"><?php echo $row['address'] ?></i></span>
                </div>
            <?php } ?>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">服务类型：</label>
                <span class="weui-form-preview__value"><i class="fa  fa-drivers-license-o red"></i> <?php echo getRepairMode($row['apply_device']) ?></span>
            </div>


            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">施工要求：</label>
                <span class="weui-form-preview__value"><?php echo $row['content'] ?></span></div>


            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">项目经理：</label>
                <span class="weui-form-preview__value">

            <?php if (!empty($row['pmid'])) {
                echo ' <i class="fa  fa-user-o red"></i>      ' . getManage($row['pmid']);
            }
            ?>
        
        </span></div>

            <?php if (!empty($row['begintime'])) { ?>
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">工程周期：</label>
                    <span class="weui-form-preview__value"><i class="fa fa-play green"></i> <?php echo date("Y-m-d ", $row['begintime']) . "
		<br/><i  class=' red fa  fa-stop'></i>     " . date("Y-m-d", $row['endtime']) ?></span></div>
            <?php } ?>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">施工图纸</label>
                <div class="weui-uploader">
                    <div class="weui-uploader__bd">
                        <ul class="weui-uploader__files" id="pic_uploaderFiles">
                            <?php echo $pic_lists; ?>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="sub_btn"></div>
            <input type="hidden" name="pmid" id="pmid" value="<?php echo $pmid; ?>">
            <input type="hidden" id="latitude" name="latitude">
            <input type="hidden" id="longitude" name="longitude">
            <input type="hidden" id="accuracy" name="accuracy">
            <input type="hidden" id="address" name="address">
            <input type="hidden" id="address_name" name="address_name" value="">
        </div>
    </div>
</div>

<!--项目施工 施工计划 开始 -->
<?php if ( $uid == $pmid) { ?>
    <div class="main">
        <div class="weui-cells__title "><i class="fa fa-id-card-o" aria-hidden="true"></i>工程周汇总：</div>
        <!--完修模块-->
        <form name="form" id="form" method="post" action="">
            <div class="weui-cells weui-cells_form">

                <!--上传 开始-->
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <div class="weui-uploader">
                            <div class="weui-uploader__hd">
                                <p class="weui-uploader__title">周汇总图片<span class="red">*</span></p>
                                <div class="weui-uploader__info">0/9</div>
                            </div>

                            <div class="weui-uploader__bd">
                                <ul class="weui-uploader__files" id="uploaderFiles">

                                </ul>
                                <div class="weui-uploader__input-box">
                                    <input id="uploaderInput" class="weui-uploader__input">
                                    <input type="hidden" name="picfiles" id="picfiles">
                                </div>
                            </div>
                            <div id="tip" class=" alert-error manage-alert manage-alert-error">请上传周施工汇总照片</div>
                        </div>
                    </div>
                </div>
                <!--上传 结束-->

                <div class="weui-cells__title">汇总备注：</div>
                <div class="weui-cells ">
                    <div class="weui-cell">
                        <div class="weui-cell__bd">
                            <textarea class="form-control" name="content2" id="content2" placeholder="请输入文本"
                                      rows="3"></textarea>
                            <div class="weui-textarea-counter"><span>0</span>/200</div>
                        </div>
                    </div>
                </div>

            </div>
            <!--上传 开始-->

            <!--上传 结束-->
            <input type="hidden" name="action" id="action" value="add"/>

        </form>
        <!-- 完修模块-->
        <div class="sub_btn"><a href="javascript:;" class="weui-btn weui-btn_warn"
                                onclick="report(<?php echo $projectid ?>);return false;">上传施工周汇总</a></div>
        <div class="clear"></div>
    </div>
    <!--项目施工签到 结束-->
<?php } ?>


<div class="sub_btn"></div>



<div class="clear"></div>
<div class="sub_btn"></div>
<script src="lib/jquery-2.1.4.js"></script>
<script src="lib/fastclick.js"></script>
<script src="js/layer/layer.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
    $(function () {
        FastClick.attach(document.body);


    });
    /*
     * 注意：
     * 1. 所有的JS接口只能在公众号绑定的域名下调用，公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
     * 2. 如果发现在 Android 不能分享自定义内容，请到官网下载最新的包覆盖安装，Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
     * 3. 常见问题及完整 JS-SDK 文档地址：http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
     *
     * 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决，如仍未能解决可通过以下渠道反馈：
     * 邮箱地址：weixin-open@qq.com
     * 邮件主题：【微信JS-SDK反馈】具体问题
     * 邮件内容说明：用简明的语言描述问题所在，并交代清楚遇到该问题的场景，可附上截屏图片，微信团队会尽快处理你的反馈。
     */
    wx.config({
        debug: false,
        appId: '<?php echo $signPackage["appId"];?>',
        timestamp: <?php echo $signPackage["timestamp"];?>,
        nonceStr: '<?php echo $signPackage["nonceStr"];?>',
        signature: '<?php echo $signPackage["signature"];?>',
        jsApiList: [
            // 所有要调用的 API 都要加到这个列表中
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'openAddress',
            'chooseImage',
            'previewImage',
            'uploadImage',
            'downloadImage',
            'openLocation',
            'getLocation'
        ]
    });
    wx.ready(function () {
        //预览图片
        var images = $("#pic_uploaderFiles").find("img");

        var imgPaths = [];
        images.each(function () {
            imgPaths.push(this.src);
        });
        $("#pic_uploaderFiles").on("click", function () {
            current = imgPaths[0];
            console.log(current);
            wx.previewImage({
                current: current,
                urls: imgPaths
            });
        });

        wx.getLocation({
            type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
            success: function (res) {
                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                $("#latitude").val(latitude);
                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                $("#longitude").val(longitude);
                var speed = res.speed; // 速度，以米/每秒计
                var accuracy = res.accuracy; // 位置精度
                $("#accuracy").val(accuracy);
                if (latitude != '0.0') {
                    $.ajax({
                        url: "getMap.php?l=" + latitude + "&w=" + longitude,
                        type: "GET",
                        timeout: 2000,
                        dataType: "json",
                        success: function (data) {
                            console.log('ok');
                            var gps_address = data.result.address;
                            var address_name = data.result.formatted_addresses.recommend;
                            //alert(gps_address);
                            $("#address").val(gps_address);
                            $("#address_name").val(address_name);
                        },
                        error: function (data) {
                            console.log('error');

                        }
                    });
                } else {
                    $.alert("请把gps开启为当前地点", " 温馨提示");
                }
            },
            cancel: function (res) {
                $.alert("你拒绝授权获取地理位置，我们的工程师无法快速定位。时效性可能会延时，请谅解", " 温馨提示");
            }

        });


        //打开相册
        var images = {
            localId: [],
            serverId: []
        };
        var filepaths = [];
        document.querySelector('#uploaderInput').onclick = function () {
            wx.chooseImage({
                count: 9, // 默认9
                sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    var picurls = res.localIds;
                    var uploadCount = 0;
                    var pic_length = res.localIds.length;
                    var m = 0;
                    var upload = function () {
                        wx.uploadImage({
                            localId: picurls[uploadCount],
                            success: function (res) {
                                //如果还有照片，继续上传
                                var serverId = res.serverId; // 返回图片的服务器端ID
                                console.log(serverId);
                                $.ajax({
                                    type: 'POST',
                                    url: "getFile.php",
                                    data: {serverId: serverId},
                                    dataType: 'JSON',
                                    success: function (result) {
                                        if (result.errno == 0) {
                                            console.log(result.msg);
                                            if (result.msg === "ok") {
                                                filepaths.push(result.filename);
                                                $("#picfiles").val(filepaths);
                                                $("#uploaderFiles").append('<li class="weui-uploader__file" style="background-image:url(' + result.filename + ')"></li>');
                                            } else {
                                                $.alert("上传失败<br/> 请联系管理员！", "温馨提示");
                                            }
                                        }
                                    }
                                });
                                uploadCount++;
                                if (uploadCount < pic_length) {
                                    upload();
                                }
                            }
                        });
                    }
                    upload();
                }
            });
        }
        //


    })


    // 签到
    function report(id) {
        var pmid = $("#pmid").val();
        var latitude = $("#latitude").val();
        var longitude = $("#longitude").val();
        var accuracy = $("#accuracy").val();
        var address = $("#address").val();
        var address_name = $("#address_name").val();
        var content2 = $("#content2").val();
        var picfiles = $("#picfiles").val();

        if (picfiles.length == 0) {
            $.alert("请上传照片", "温馨提示");
            return false;
        }

        if (latitude !== "") {
            $.ajax({
                type: 'POST',
                url: "project_do.php",
                data: {
                    id: id,
                    op: 'report',
                    'pmid': pmid,
                    'latitude': latitude,
                    'longitude': longitude,
                    'accuracy': accuracy,
                    'address': address,
                    'address_name': address_name,
                    'remark': content2,
                    'picfiles': picfiles,
                },
                dataType: 'JSON',
                success: function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        console.log(result.msg);

                        $.toast("提交周汇总成功", function () {
                            window.location.href = 'projectDetail.php?projectid='+id;
                        });


                    } else {
                        $.alert(result.msg);
                    }
                }
            });
        } else {
            $.alert("获取不到位置信息", "温馨提示");
        }
    }



</script>
<script src="js/jquery-weui.js"></script>
<?php require_once('footer.php'); ?>